package com.ruoqing.system.rest;

import com.alibaba.fastjson.JSONObject;
import com.ruoqing.core.constant.Constants;
import com.ruoqing.core.constant.GoogleConstants;
import com.ruoqing.core.dto.BaseResponse;
import com.ruoqing.core.dto.auth.AuthDto;
import com.ruoqing.core.dto.auth.LoginUserDto;
import com.ruoqing.core.dto.auth.TokenDto;
import com.ruoqing.utils.common.JsonUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
	* @author YaoXian
	* @title: Oauth2UserLogin
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 请求认证服务器获取Token
	* @date 2020-12-17 22:46
	*/
@Component
public class Oauth2UserLogin {
		
		public AuthDto getUserToken(String grantType, String clientId, String clientSecret, LoginUserDto loginUserDto){
				String status;
				String errorMsg;
				RestTemplate restTemplate = new RestTemplate();
				MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>(5);
				paramMap.add("grant_type", grantType);
				paramMap.add("client_id",clientId);
				paramMap.add("client_secret",clientSecret);
				paramMap.add("username",loginUserDto.getUsername());
				paramMap.add("password",loginUserDto.getPassword());
				JSONObject jsonObject = null;
				try {
						jsonObject = restTemplate.postForObject("http://localhost:9999/oauth/token", paramMap, JSONObject.class);
				}catch (Exception e){
						if (e.getMessage().contains("Bad credentials")){
								status = Constants.responseError;
								errorMsg = "认证失败,密码错误!";
								return AuthDto.buildAuthDto(status,errorMsg);
						}else if (e.getMessage().contains("401")){
								status = Constants.responseError;
								errorMsg = "认证失败,用户名错误或不存在!";
								return AuthDto.buildAuthDto(status,errorMsg);
						}
				}
				status = Constants.responseOk;
				TokenDto tokenDto = (TokenDto) JsonUtils.jsonToObject(jsonObject.toString(), TokenDto.class);
				return AuthDto.buildAuthDto(status,tokenDto);
		}
		
		public BaseResponse googleCheck(String googleToken){
				String status = "ok";
				HttpHeaders headers = new HttpHeaders();
				headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
				//设置接收返回值的格式为json
				List<MediaType> mediaTypeList = new ArrayList<MediaType>();
				mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
				headers.setAccept(mediaTypeList);
				HttpEntity<String> entity = new HttpEntity<String>(null, headers);
				RestTemplate restTemplate = new RestTemplate();
				JSONObject forObject = restTemplate.getForObject(GoogleConstants.CHECK_URL + "?secret=" + GoogleConstants.CHECK_SECRET + "&response=" + googleToken, JSONObject.class,entity);
				if (StringUtils.isEmpty(forObject)){
						status = Constants.responseError;
				}else {
						if (forObject.getBooleanValue("success")){
								status = Constants.responseOk;
						}else {
								status = Constants.responseError;
						}
				}
				return BaseResponse.buildResponse(status);
		}
}
